// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   ContactExpiry.java

package include.auto_execute;

import include.nseer_db.nseer_db_backup;
import java.io.PrintStream;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletContext;

public class ContactExpiry
{

	private nseer_db_backup nseer_db;
	private nseer_db_backup db;
	private ResultSet rs;
	private ResultSet rs1;
	private String sql1;
	private String date;
	private String idgroup[];
	private String namegroup[];
	private String alarmgroup[];
	private String dategroup[];

	public ContactExpiry()
	{
		rs = null;
		rs1 = null;
		sql1 = "";
		date = "";
		idgroup = new String[0x186a0];
		namegroup = new String[0x186a0];
		alarmgroup = new String[0x186a0];
		dategroup = new String[0x186a0];
	}

	public void flow(ServletContext dbApplication)
	{
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date now = new Date();
		try
		{
			nseer_db_backup dbd = new nseer_db_backup(dbApplication);
			if (dbd.conn("mysql"))
			{
				String sqldb = "show databases";
				for (ResultSet rsdb = dbd.executeQuery(sqldb); rsdb.next();)
					if (rsdb.getString("database").equals("ondemand1"))
					{
						nseer_db = new nseer_db_backup(dbApplication);
						if (nseer_db.conn(rsdb.getString("database")))
						{
							String sql3 = "delete from crm_alarm_contact_expiry";
							nseer_db.executeUpdate(sql3);
							String sql = "select * from crm_file where check_tag='1'";
							rs = nseer_db.executeQuery(sql);
							int i;
							for (i = 0; rs.next(); i++)
							{
								idgroup[i] = rs.getString("customer_ID");
								namegroup[i] = rs.getString("customer_name");
								alarmgroup[i] = rs.getString("contact_period_limit");
								dategroup[i] = rs.getString("register_time");
							}

							for (int j = 0; j < i; j++)
							{
								sql1 = (new StringBuilder("select * from crm_contact where customer_ID='")).append(idgroup[j]).append("' order by contact_time desc").toString();
								rs1 = nseer_db.executeQuery(sql1);
								if (rs1.next())
									date = rs1.getString("contact_time");
								else
									date = dategroup[j];
								if (!date.equals(""))
								{
									Date date1 = formatter.parse(date);
									long days1 = (long)((double)((now.getTime() - date1.getTime()) / 0x5265c00L) + 0.5D);
									long Time = date1.getTime() / 1000L + (long)(0x15180 * Integer.parseInt(alarmgroup[j]));
									date1.setTime(Time * 1000L);
									if (date1.getTime() - now.getTime() < 0L)
									{
										long days = (long)((double)((now.getTime() - date1.getTime()) / 0x5265c00L) + 0.5D);
										String sql2 = (new StringBuilder("insert into crm_alarm_contact_expiry(customer_ID,customer_name,lately_contact_time,contact_period_limit,contact_period_expiry,period_expiry_over) values('")).append(idgroup[j]).append("','").append(namegroup[j]).append("','").append(date).append("','").append(alarmgroup[j]).append("','").append(days1).append("','").append(days).append("')").toString();
										nseer_db.executeUpdate(sql2);
									}
								}
							}

							nseer_db.close();
						} else
						{
							System.out.println("i am sorry!");
						}
					}

				dbd.close();
			} else
			{
				System.out.println("i am sorry!");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
